/* global Module */

/* Magic Mirror
 * Module: jsoninfo
 *
 * By Michael Teeuw http://michaelteeuw.nl
 * MIT Licensed.
 */
 

Module.register("MMM-ReceiveData",{

	// Default module config.
	defaults: {
		//text: "Hello World!"
		animationSpeed:2000
	},

	// Override start method.
	start: function() {
		Log.log("Starting module: " + this.name);
		this.jsonData = [];
		this.loaded = false;
		this.sendSocketNotification("INIT", {});
	},
	// Define required css.
	getStyles: function() {
		return ["MMM-ReceiveData.css"];
	},
	// Override required translations.
	getTranslations: function() {
		return {
			zh_cn: "translations/zh_cn.json"
		};
	},
	// Override dom generator.
	getDom: function() {	
			
		var wrapper = document.createElement("table");
		wrapper.className = "small";
		if (this.jsonData.length === 0) {
			wrapper.innerHTML = this.translate("WAITING");
			wrapper.className = "small dimmed";
			return wrapper;
		}
		for (var f in this.jsonData) {
			var item = this.jsonData[f];
			var itemWrapper = document.createElement("tr");
			itemWrapper.className = "normal";
			var titleWrapper = document.createElement("td");
			titleWrapper.innerHTML = item.title;
			titleWrapper.className = "title bright";
			itemWrapper.appendChild(titleWrapper);
			var valueWrapper =  document.createElement("td");
			valueWrapper.innerHTML =item.value;
			valueWrapper.className = "time light";
			itemWrapper.appendChild(valueWrapper);
			wrapper.appendChild(itemWrapper);
		}
		return wrapper;
	},
	// Subclass socketNotificationReceived received.
	socketNotificationReceived: function(notification, payload) {
		
		if (notification === "UPDATE_DATA") {
			this.jsonData=payload.jsonData;
			this.loaded = true;
			this.updateDom(this.config.animationSpeed);
		}
	}
});
